using System;
using System.Collections.Generic;
using System.Text;
using csvorbis;

namespace OpenALDotNet.Streams
{
        /// <summary>
        /// Nowhere near complete. just a demo.
        /// </summary>
    public class VorbisAudioStream : AudioStream
    {
        VorbisFile vorbisfile;
        public VorbisAudioStream(string filename)
        {
            this.vorbisfile = new VorbisFile(filename);
        }

        public string[] Comments
        {
            get
            {
                int commentNumber = 0;
                Comment[] comments = vorbisfile.getComment();
                foreach (Comment c in comments)
                    commentNumber+=c.comments;
                string [] commentTexts=new string[commentNumber];
                commentNumber = 0;
                foreach(Comment c in comments)
                {
                    int[] comLengths = c.comment_lengths;
                    for (int i = 0; i < c.comments; i++)
                        commentTexts[commentNumber++] = c.getComment(i).Substring(0, comLengths[i]);                
                }
                return commentTexts;
            }
        }

        public override AudioFormatEnum Format
        {
            get
            {
                return AudioBuffer.GetFormat(16, vorbisfile.getInfo()[0].channels);
            }
        }

        public override int Frequency
        {
            get
            {
                return vorbisfile.getInfo()[0].rate;
            }
        }

        public override int Read(byte[] buffer, int offset, int count)
        {
            int[] bitStream = new int[1];
            if (offset != 0)
            {
                int newcount = buffer.Length - offset;
                Byte[] buffer2 = new byte[newcount];
                int read = vorbisfile.read(buffer2, newcount, 0, 2, 1, bitStream);
                buffer2.CopyTo(buffer, offset);
                return read;
            }
            else
            {
                return vorbisfile.read(buffer, count, 0, 2, 1, bitStream);
            }
        }


        public override bool CanSeekTime
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override TimeSpan TimePosition
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        public override TimeSpan TimeSeek(TimeSpan offset, System.IO.SeekOrigin origin)
        {

            throw new Exception("The method or operation is not implemented.");
        }

        public override TimeSpan TimeLength
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override bool CanSeekTrack
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override int TrackPosition
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }

        public override int TrackSeek(int offset, System.IO.SeekOrigin origin)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override int TrackCount
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override bool CanRead
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override bool CanSeek
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override long Length
        {
            get { throw new Exception("The method or operation is not implemented."); }
        }

        public override long Position
        {
            get
            {
                throw new Exception("The method or operation is not implemented.");
            }
            set
            {
                throw new Exception("The method or operation is not implemented.");
            }
        }


        public override long Seek(long offset, System.IO.SeekOrigin origin)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override bool CanWrite
        {
            get { return false; }
        }

        public override void Flush()
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override void SetLength(long value)
        {
            throw new Exception("The method or operation is not implemented.");
        }

        public override void Write(byte[] buffer, int offset, int count)
        {
            throw new Exception("The method or operation is not implemented.");
        }
        protected override void Dispose(bool disposing)
        {
            base.Dispose(disposing);
        }
    }

}
